$(document).ready(function() {
    
	// auto complete
    //$("#app_member_id").select2();
	$("#app_member_id").select2({
		placeholder: "Select Client"
	});
	
	$("#property_id").select2();
	
	$(".fa").click(function(){
         $('#app_date').datepicker({
		 format: "dd/mm/yyyy",
                // startDate: '+1d'
		 });
    });
    $('#app_date').datepicker({
            format: "dd/mm/yyyy",
           // startDate: '+1d',
            autoclose: true,
          //  todayHighlight: true
     });
     
    //$('.timepicker-default').timepicker();
   /* $('.timepicker-24').timepicker({
                minuteStep: 1,
                showSeconds: false,
                showMeridian: false,
                defaultTime: '00:00'
     });*/
    $('.clockpicker ').clockpicker({
        autoclose: true
    });
	
	
	$('.property-select').change(function() {

        var postdata = {};
        var property_type = $(this).val();
       
            postdata['property_type'] = property_type;
            jQuery.post(BASE_PATH + 'property/getpropertysublist', postdata, function(result) {
                $('.property-subtype-select').html(result);
                
            });
    });
	
	$('.property-subtype-select').change(function() {
		
        var postdata = {};
		var property_subtype = $(this).val();
        var property_type = $('.property-select').val();
				
            postdata['property_type'] = property_type;
			
			postdata['property_subtype'] = property_subtype;
			
            jQuery.post(BASE_PATH + 'appointment/getpropertylist', postdata, function(result) {
                $('.property-select-property').html(result);  
            });
    });
	
	
	// open popup in add Appointment
	
	$('#btn-new-appointment').click( function() {
	
		$("#app_member_id").select2("val", '');
		$("#property_id").select2("val", '');
		
		$("#app_date").val($.datepicker.formatDate("dd/mm/yy", new Date()));
		$("#prp_id").val('');
		$("#app_note").val('');
		$("#add_btn_appointment").html('Submit');
		$("#myModalLabel").html('Add Appointment');
		//$('#new-ticket-wrapper').slideToggle("fast","linear")
		$("#myModal_Appointment").modal('show');
	});
	
	$('.edit_appointment').click( function() {
	
	//$('#new-ticket-wrapper').slideToggle("fast","linear")
		
		var app_id = $(this).attr('app_id');
		
        var app_member_id = $(this).attr('app_member_id');
		var ref = $(this).attr('ref');
        var prp_id = $(this).attr('prp_id');
        var app_date = $(this).attr('app_date');
		var app_time = $(this).attr('app_time');
        var app_note = $(this).attr('app_note');
        
		$("#app_id").val(app_id);
		
		$("#app_member_id").select2("val", app_member_id);
		$("#property_id").select2("val", prp_id);
        $("#app_date").val(app_date);
		$("#app_time").val(app_time);
        $("#app_note").val(app_note);
		
		$("#error_add_appointment").hide();
		if (app_member_id == '') {
			$("#error_add_appointment").show();
			$("#error_add_appointment").html('Please select Client');
			$("#app_member_id").css('border-color', 'red');
			return false;
		} else if (prp_id == '') {
			$("#error_add_appointment").show();
			$("#error_add_appointment").html('Please select Property');
			$("#property_id").css('border-color', 'red');
			return false;
		}
		
        $("#add_btn_appointment").html('Update');
        $("#myModalLabel").html('Edit Appointment for '+ ref);
		$("#myModal_Appointment").modal('show');
	});
	
	$("#close_btn_appointment").click(function(){
		$("#app_member_id").val('');
		$("#prp_id").val('');
		$("#app_date").val('');
		$("#app_note").val('');
	});
	
	$("#add_btn_appointment").click(function() {
	
		var app_id = $("#app_id").val();
		var app_member_id = $("#app_member_id").val();
		//var property_type = $("#property_type").val();
		//var property_subtype = $("#property_subtype").val();
		var prp_id = $("#property_id").val();
		var app_date = $("#app_date").val();
		var app_time = $("#app_time").val();
		var app_note = $("#app_note").val();

		$("#error_add_appointment").hide();
		if (app_member_id == '') {
			$("#error_add_appointment").show();
			$("#error_add_appointment").html('Please select Client');
			$("#app_member_id").css('border-color', 'red');
			return false;
		} /*else if (property_type == '') {
			$("#error_add_appointment").show();
			$("#error_add_appointment").html('Please select Property Type');
			$("#property_type").css('border-color', 'red');
			return false;
		} else if (property_subtype == '') {
			$("#error_add_appointment").show();
			$("#error_add_appointment").html('Please select Property SubType');
			$("#property_subtype").css('border-color', 'red');
			return false;
		}*/ else if (prp_id == '') {
			$("#error_add_appointment").show();
			$("#error_add_appointment").html('Please select Property');
			$("#property_id").css('border-color', 'red');
			return false;
		} else {
			var postdata = {};
			var obj = $(this);
			
			postdata['app_id'] = app_id;
			postdata['app_member_id'] = app_member_id;
			//postdata['property_type'] = property_type;
			//postdata['property_subtype'] = property_subtype;
			postdata['prp_id'] = prp_id;
			postdata['app_date'] = app_date;
			postdata['app_time'] = app_time;
			postdata['app_note'] = app_note;
			//alert(postdata);
			jQuery.post(BASE_PATH + '/appointment/getappointmentform', postdata, function(result) {
				if (result == 1) {
					//alert(success_add_appointment);
					$("#success_add_appointment").show();
					//alert('imnnnn');
					window.setTimeout(function() {
						 //$("#member_id").select2("val","");
						 //$(".chek_all").prop("checked", false);
						 $("#success_add_appointment").hide();
						 $("#myModal_Appointment").modal('hide');
						  window.location = BASE_PATH + 'appointment/list';
					}, 3000);
				}
			});
		}
	});
     
});
	 